package com.xs;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.xs.client.entity.RpcRequest;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;

public class SimpleEncoder extends MessageToMessageEncoder<CustomProtcol> {

	
	
	private Class<?> genericClass;
	
	/**
	 * 
	 */
	public SimpleEncoder(Class<?> genericClass) {
		this.genericClass = genericClass;
	}
	
	
	
	@Override
	protected void encode(ChannelHandlerContext ctx, CustomProtcol msg, List<Object> out) throws Exception {

		if (msg != null && msg.getCommand() != 0 && msg.getBody() != null) {
			ByteBuf buf = ctx.alloc().buffer();
			buf.writeInt(msg.getCommand());
			Object req = msg.getBody();
			String reqStr = JSON.toJSONString(req);
			byte reqByte[] = reqStr.getBytes();
			buf.writeInt(reqByte.length);
			buf.writeBytes(reqByte);
			out.add(buf);
		}
	}

}
